package com.puyuba.puyuba.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.puyuba.puyuba.R;
import com.puyuba.puyuba.bean.NeedBean;
import com.pyb.java.cache.CacheUserInfo;
import com.pyb.java.network.RequestBodyUtil;
import com.pyb.java.network.ResponseEntity;
import com.pyb.java.network.RetrofitManger;
import com.pyb.java.network.observers.ResponseObserve;

import com.pyb.java.network.scheduler.RxSchedulers;
import com.puyuba.puyuba.network.UserApi;
import com.pyb.java.commonutil.utils.StringUtil;
import com.pyb.java.commonutil.ToastHelper;


import java.util.HashMap;

import io.reactivex.disposables.Disposable;
import okhttp3.RequestBody;

/**
 * 学历提升需求dialog
 */

public class EducationDemandDialog extends Dialog {

    private RelativeLayout rlBg;
    private TextView tvCommit;
    private ImageView ivClose;
    private TextView tvCurrentEdu1;
    private TextView tvCurrentEdu2;
    private TextView tvCurrentEdu3;
    private TextView tvCurrentEdu4;
    private TextView tvTargetEdu1;
    private TextView tvTargetEdu2;
    private TextView tvTime1;
    private TextView tvTime2;
    private TextView tvTime3;
    private TextView tvTime4;
    private LinearLayout llEasy;
    private LinearLayout llDifficulty;
    private String currentEdu = "";//当前学历
    private String targetEdu = "";//目标学历
    private String time = "";//拿证时间
    private int easy = 0;//难易程度

    public EducationDemandDialog(Context context) {
        super(context, R.style.ContactDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_education_demand_layout);
        initView();
        //初始化界面控件的事件
        initEvent();
        getneed(2, CacheUserInfo.getInstance().getHxUid());
    }

    public void setData(NeedBean data) {
        if (data.getNow_edu().equals("高中")){
            tvCurrentEdu1.setBackground(getContext().getResources().getDrawable(R.drawable.bg_text_select));
            tvCurrentEdu1.setTextColor(getContext().getColor(R.color.color_181C2F));
            currentEdu = tvCurrentEdu1.getText().toString();
        }else if (data.getNow_edu().equals("职高")){
            tvCurrentEdu2.setBackground(getContext().getResources().getDrawable(R.drawable.bg_text_select));
            tvCurrentEdu2.setTextColor(getContext().getColor(R.color.color_181C2F));
            currentEdu = tvCurrentEdu2.getText().toString();
        }else if (data.getNow_edu().equals("中专")){
            tvCurrentEdu3.setBackground(getContext().getResources().getDrawable(R.drawable.bg_text_select));
            tvCurrentEdu3.setTextColor(getContext().getColor(R.color.color_181C2F));
            currentEdu = tvCurrentEdu3.getText().toString();
        }else if (data.getNow_edu().equals("大专")){
            tvCurrentEdu4.setBackground(getContext().getResources().getDrawable(R.drawable.bg_text_select));
            tvCurrentEdu4.setTextColor(getContext().getColor(R.color.color_181C2F));
            currentEdu = tvCurrentEdu4.getText().toString();
        }
        if (data.getAim_edu().equals("大专")){
            tvTargetEdu1.setBackground(getContext().getResources().getDrawable(R.drawable.bg_text_select));
            tvTargetEdu1.setTextColor(getContext().getColor(R.color.color_181C2F));
            targetEdu = tvTargetEdu1.getText().toString();
        }else if (data.getAim_edu().equals("本科")){
            tvTargetEdu2.setBackground(getContext().getResources().getDrawable(R.drawable.bg_text_select));
            tvTargetEdu2.setTextColor(getContext().getColor(R.color.color_181C2F));
            targetEdu = tvTargetEdu2.getText().toString();
        }

        if (data.getYear().equals("0.5年")){
            tvTime1.setBackground(getContext().getResources().getDrawable(R.drawable.bg_text_select));
            tvTime1.setTextColor(getContext().getColor(R.color.color_181C2F));
            time = tvTime1.getText().toString();
        }else if (data.getYear().equals("1年")){
            tvTime2.setBackground(getContext().getResources().getDrawable(R.drawable.bg_text_select));
            tvTime2.setTextColor(getContext().getColor(R.color.color_181C2F));
            time = tvTime2.getText().toString();
        }else if (data.getYear().equals("1.5年")){
            tvTime3.setBackground(getContext().getResources().getDrawable(R.drawable.bg_text_select));
            tvTime3.setTextColor(getContext().getColor(R.color.color_181C2F));
            time = tvTime3.getText().toString();
        }else if (data.getYear().equals("2年")){
            tvTime4.setBackground(getContext().getResources().getDrawable(R.drawable.bg_text_select));
            tvTime4.setTextColor(getContext().getColor(R.color.color_181C2F));
            time = tvTime4.getText().toString();
        }

        if (data.getDiff_level() == 1){
            llEasy.setBackground(getContext().getResources().getDrawable(R.drawable.bg_text_select));
            easy = 1;
        }else if (data.getDiff_level() == 2){
            llDifficulty.setBackground(getContext().getResources().getDrawable(R.drawable.bg_text_select));
            easy = 2;
        }

    }
    //查询学生提交需求
    public void getneed(int type,String uid) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("type", type);
        map.put("uid", uid);
        RequestBody bodyByMap = RequestBodyUtil.getRequestBodyByMap(map);
        RetrofitManger.getInstance().createService(UserApi.class)
                .getneed(bodyByMap)
                .compose(RxSchedulers.<ResponseEntity<NeedBean>>applySchedulers())
                .subscribe(new ResponseObserve<NeedBean>(){

                    @Override
                    public void onSuccess(ResponseEntity<NeedBean> responseEntity) {
                        setData(responseEntity.getData());
                    }

                    @Override
                    public void onFail(String errorMsg) {
                        if (errorMsg != null&&!errorMsg.equals("")){
                            ToastHelper.show(errorMsg);
                        }else {
                            ToastHelper.show("服务器繁忙，请稍后再试");
                        }
                    }

                    @Override
                    public void onSubscribeStart(@NonNull Disposable disposable) { }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }

                });
    }
    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        ivClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        rlBg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        tvCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ( onClickBottomListener!= null) {
                    if (!StringUtil.isEmpty(currentEdu)&&!StringUtil.isEmpty(targetEdu)&&!StringUtil.isEmpty(time)&&easy!= 0){
                        dismiss();
                        onClickBottomListener.commit(currentEdu,targetEdu,time,easy);
                    }else {
                        ToastHelper.show("请填写需求");
                    }
                }
            }
        });
        llEasy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                llEasy.setBackground(getContext().getResources().getDrawable(R.drawable.bg_text_select));
                llDifficulty.setBackground(getContext().getResources().getDrawable(R.drawable.shape_dotted_line_grey_dp9));
                easy = 1;
            }
        });
        llDifficulty.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                llDifficulty.setBackground(getContext().getResources().getDrawable(R.drawable.bg_text_select));
                llEasy.setBackground(getContext().getResources().getDrawable(R.drawable.shape_dotted_line_grey_dp9));
                easy = 2;
            }
        });
        tvTargetEdu1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvTargetEdu1.setBackground(getContext().getResources().getDrawable(R.drawable.bg_text_select));
                tvTargetEdu2.setBackground(null);
                targetEdu = "大专";
            }
        });
        tvTargetEdu2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvTargetEdu2.setBackground(getContext().getResources().getDrawable(R.drawable.bg_text_select));
                tvTargetEdu1.setBackground(null);
                targetEdu = "本科";
            }
        });
        tvCurrentEdu1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvCurrentEdu1.setBackground(getContext().getResources().getDrawable(R.drawable.bg_text_select));
                tvCurrentEdu2.setBackground(null);
                tvCurrentEdu3.setBackground(null);
                tvCurrentEdu4.setBackground(null);
                currentEdu = "高中";
            }
        });
        tvCurrentEdu2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvCurrentEdu2.setBackground(getContext().getResources().getDrawable(R.drawable.bg_text_select));
                tvCurrentEdu1.setBackground(null);
                tvCurrentEdu3.setBackground(null);
                tvCurrentEdu4.setBackground(null);
                currentEdu = "职高";
            }
        });
        tvCurrentEdu3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvCurrentEdu3.setBackground(getContext().getResources().getDrawable(R.drawable.bg_text_select));
                tvCurrentEdu2.setBackground(null);
                tvCurrentEdu1.setBackground(null);
                tvCurrentEdu4.setBackground(null);
                currentEdu = "中专";
            }
        });
        tvCurrentEdu4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvCurrentEdu4.setBackground(getContext().getResources().getDrawable(R.drawable.bg_text_select));
                tvCurrentEdu2.setBackground(null);
                tvCurrentEdu3.setBackground(null);
                tvCurrentEdu1.setBackground(null);
                currentEdu = "大专";
            }
        });
        tvTime1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvTime1.setBackground(getContext().getResources().getDrawable(R.drawable.bg_text_select));
                tvTime2.setBackground(null);
                tvTime3.setBackground(null);
                tvTime4.setBackground(null);
                time = tvTime1.getText().toString();
            }
        });
        tvTime2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvTime2.setBackground(getContext().getResources().getDrawable(R.drawable.bg_text_select));
                tvTime1.setBackground(null);
                tvTime3.setBackground(null);
                tvTime4.setBackground(null);
                time = tvTime2.getText().toString();
            }
        });
        tvTime3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvTime3.setBackground(getContext().getResources().getDrawable(R.drawable.bg_text_select));
                tvTime2.setBackground(null);
                tvTime1.setBackground(null);
                tvTime4.setBackground(null);
                time = tvTime3.getText().toString();
            }
        });
        tvTime4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvTime4.setBackground(getContext().getResources().getDrawable(R.drawable.bg_text_select));
                tvTime2.setBackground(null);
                tvTime1.setBackground(null);
                tvTime3.setBackground(null);
                time = tvTime4.getText().toString();
            }
        });

    }
    /**
     * 设置确定取消按钮的回调
     */
    public OnClickBottomListener onClickBottomListener;
    public EducationDemandDialog setOnClickBottomListener(OnClickBottomListener onClickBottomListener) {
        this.onClickBottomListener = onClickBottomListener;
        return this;
    }

    public interface OnClickBottomListener{

        public void commit(String currentEdu, String targetEdu, String time, int easy);

    }
    private void initView() {
        rlBg = (RelativeLayout) findViewById(R.id.rl_bg);
        tvCommit = (TextView) findViewById(R.id.tv_commit);
        ivClose = (ImageView) findViewById(R.id.iv_close);
        tvCurrentEdu1 = (TextView) findViewById(R.id.tv_current_edu1);
        tvCurrentEdu2 = (TextView) findViewById(R.id.tv_current_edu2);
        tvCurrentEdu3 = (TextView) findViewById(R.id.tv_current_edu3);
        tvCurrentEdu4 = (TextView) findViewById(R.id.tv_current_edu4);
        tvTargetEdu1 = (TextView) findViewById(R.id.tv_target_edu1);
        tvTargetEdu2 = (TextView) findViewById(R.id.tv_target_edu2);
        tvTime1 = (TextView) findViewById(R.id.tv_time1);
        tvTime2 = (TextView) findViewById(R.id.tv_time2);
        tvTime3 = (TextView) findViewById(R.id.tv_time3);
        tvTime4 = (TextView) findViewById(R.id.tv_time4);
        llEasy = (LinearLayout) findViewById(R.id.ll_easy);
        llDifficulty = (LinearLayout) findViewById(R.id.ll_difficulty);

    }


}